function sxt_flyto(e){
    // console.log("e",e)
    // var name=($(e).parent()).attr("name");
    // console.log('name',name)
    var name=e;
    poinArr.forEach((item, index) => {
        console.log('item.id',item.id)
        if (item.text == name) {
            var lon= item.position.x;
            var lat= item.position.y;
            viewer.camera.flyTo({
                destination: Cesium.Cartesian3.fromDegrees(lon, lat-0.001, 900),
                orientation: {
                    heading:Cesium.Math.toRadians(0),
                    pitch:Cesium.Math.toRadians(-60),
                    roll:0,
                }
            });
        }else{
            console.log("s")
        }
    })


}


function suofang(){
    class Rotate {
        constructor() {
            this.el = null;
        }

        init(cls,cls1) {
            this.el = document.getElementById(cls)
            this.el1=document.getElementById(cls1)
            return this
        }

        RotateWheel() {
            document.onwheel = (e) => {
                let delta = (e.wheelDelta && (e.wheelDelta > 0 ? 1 : -1))

                if (delta > 0) {//放大
                    // 向上滚
                    let oWidth = this.el.offsetWidth;//取得图片的实际宽度
                    let oHeight = this.el.offsetHeight; //取得图片的实际高度
                    if(oWidth<500){
                        this.el.style.width = (oWidth + 50) + "px"
                        this.el.style.height = (oHeight + 50 / oWidth * oHeight) + "px"
                    }


                    // 向上滚
                    let oWidth1 = this.el1.offsetWidth;//取得图片的实际宽度
                    let oHeight1 = this.el1.offsetHeight; //取得图片的实际高度
                    if(oWidth1<500){
                        this.el1.style.width = (oWidth1 + 50) + "px"
                        this.el1.style.height = (oHeight1 + 50 / oWidth * oHeight) + "px"
                    }


                } else if (delta < 0) {//缩小
                    //向下滚
                    let oWidth = this.el.offsetWidth; //取得图片的实际宽度
                    let oHeight = this.el.offsetHeight; //取得图片的实际高度
                    if (oWidth>200) {//判断如果图片缩小到原图大小就停止缩小(100和75分别为原图的宽高)
                        this.el.style.width = oWidth - 50 + "px"
                        this.el.style.height = oHeight - 50 / oWidth * oHeight + "px"
                    }
                    //向下滚
                    let oWidth1 = this.el1.offsetWidth; //取得图片的实际宽度
                    let oHeight1 = this.el1.offsetHeight; //取得图片的实际高度

                    if (oWidth1>200) {//判断如果图片缩小到原图大小就停止缩小(100和75分别为原图的宽高)
                        this.el1.style.width = oWidth1 - 50 + "px"
                        this.el1.style.height = oHeight1 - 50 / oWidth * oHeight + "px"
                    }
                }
            }
        }
    }
    new Rotate().init("bianpo","bianpoliuc").RotateWheel()
}


function DaoChu(){
    var img_id=['bianpo','bianpoliuc'];
    img_id.forEach((item,index,) =>{
        var img = document.getElementById(item)
        // 将图片的src属性作为URL地址
        var url = img.src
        var a = document.createElement('a')
        var event = new MouseEvent('click')
        a.download = name // '下载图片名称'
        a.href = url
        a.dispatchEvent(event);
    })


}

$(document).ready(function() {
    $('#IJkjc7').on('change', function (event) {
        console.log($(this)[0].checked.toString());
        if ($(this)[0].checked.toString() == 'true') {
            $('#First_Diggings_all input').prop('checked', true);
        }else{
            $('#First_Diggings_all input').prop('checked', false);
        }
    });
    $("tbody").on("click","#qhbutton",function (){
        alert("暂无历史视频数据")
        console.log(1)
    });


})
//关闭所有摄像头

var shiplis=[
    "2023-02-22","2023-02-23","2023-02-24","2023-02-25","2023-02-26","2023-02-27","2023-02-28","2023-03-01","2023-03-02","2023-03-04","2023-03-05"
]
function buttonclick(){
    var value=$("#test2").val();
    $("#lssp div").remove()
    $("#lssp span").remove()
    // console.log(value)
    if (value){
        $("#lssp").show();
        var hhtml=`<span>请选择历史视频</span>`
        $("#lssp").append(hhtml)
        var durdata=value.split("~")

        var firstdate=new Date(durdata[0])
        var seconddate=new Date(durdata[1])
        console.log(firstdate<=seconddate)
        if (firstdate<=seconddate){
            $.each(shiplis,function (item,index){
                var divdate=new Date(index)
                //console.log(divdate)
                if(divdate>=firstdate && divdate<=seconddate){
                    var divhtml=`<div id="lisp${item}">${index}</div>`
                    $("#lssp").append(divhtml)
                    $(`#lisp${item}`).on("click",function (){
                        alert("暂无该时间段历史视频")
                    })
                }

            })
        }else {
            $.each(shiplis,function (item,index){
                if(item<=durdata[0]&&item>=durdata[1]){
                    var divhtml=`<div id="lisp${item}">${index}</div>`
                    $("#lssp").append(divhtml)
                    $(`#lisp${item}`).on("click",function (){
                        alert("暂无该时间段历史视频")
                    })
                }

            })
        }

    }else{
        alert("请选择日期")
    }
}

function buttonclick1(){
// 获取video元素
    var video = $('#videoid');

// 更改src属性
    video.attr('src', './data/mubiaoshibie.mp4');
}

function close_allsxt(){
    $('#First_Diggings_all input').prop('checked', false);
}
//添加摄像头详细列表
function Add_sxt_information(pick) {
    var position=pick.primitive._position;
    var cartographic = Cesium.Cartographic.fromCartesian(position);
    var longitude = Cesium.Math.toDegrees(cartographic.longitude);
    var latitude = Cesium.Math.toDegrees(cartographic.latitude);
    console.log(pick.id)
    htmlstr1='';
    htmlstr1 += '<li >' +"摄像头编号："+pick.id._label._text+'</li>';
    htmlstr1 += '<li >' +"地址：中国XX省XX市"+ '</li>';
    htmlstr1 += '<li >' +"负责人：XXX"+ '</li>';
    htmlstr1 += '<li >' +"经度："+longitude.toFixed(8)+ '</li>';
    htmlstr1 += '<li >' +"维度："+latitude.toFixed(8)+ '</li>';
    htmlstr1 +=`<input className="layui-input" id="test2" type="text" placeholder="选择日期">`;
    htmlstr1 += '<button id="qhbutton" class="qhbutton" onclick="buttonclick()">' +"切换历史视频"+ '</button>';
    htmlstr1 += '<button id="qhbutton1" class="qhbutton" onclick="buttonclick1()">' +"目标识别视频"+ '</button>';
    htmlstr1 += '</il>';
    $("#sxt_information_ul").html(htmlstr1);

    // layui.use('laydate', function(){
    //     var laydate = layui.laydate;
    //     laydate.render({
    //         elem: '#test2'
    //         ,type: 'datetime'
    //     });
    // });

    //日期范围
    layui.use('laydate', function(){
        var laydate = layui.laydate;
       laydate.render({
           elem: '#test2',
           range: '~',
           done: function(value, date, endDate){
               //点击日期、清空、现在、确定均会触发。回调返回三个参数，分别代表：生成的值、日期时间对象、结束的日期时间对象
                if(value){
                   let strArr = value.split('~');
                   self.data.start = strArr[0];
                   self.data.end = strArr[1];
                }else{
                   self.data.start = '';
                    self.data.end = '';
                }

        }
    });
    });
}


//围栏坐标
// var WL_data1=[119.10307546650772,45.918545366858616,119.12243793204446,45.91833270009977,119.11958248110989,45.9204313291319,119.11839591151342,45.92194142813643,
//     119.11843198644647,45.92294609273017,119.1069792242341,45.92294396982036,119.10648378430835,45.92157598261410,119.10307546650772,45.918545366858616]

    // 119.11843198644647,45.92294609273017,119.1069792242341,45.92294396982036,119.10648378430835,45.92157598261410,119.10307546650772,45.918545366858616 ];
var WL_data1=[119.1398270743929,45.919760733646314,119.14508543181663,45.91738008873629,119.15004447656804,45.92044714267507,119.14597005818142,45.922136674267314,119.1398270743929,45.919760733646314]
var WL_data2=[119.12264434110969,45.9181474257262,119.13298232283755,45.92925266254257,119.12912237070587,45.9285625808548,119.1209274529841,45.920830761241746,119.12264434110969,45.9181474257262];

//边坡数据
var Add_wl_data = [{
    id:'wl1',
    name:"一号边坡",
    position: WL_data1,
    maxH:[100, 100,50,0,-50,-50,0,100],
    minH:[0, 0,-50,-100,-150,-150,-100,0]
}, {
    id:'wl2',
    name:"二号边坡",
    position:WL_data2 ,
    maxH:[100, 100,50,50,100],
    minH:[0, 0,-50,-50,0]
},
]


//加载边坡
function Add_WL(){
    // undergroundSpace();
    window.tilesets_WL_Group.forEach((item,index)=>{
        viewer.entities.remove(item);
    })
    Add_wl_data.forEach((item,index)=>{
        window.wall_Wl=viewer.entities.add({
            id:item.id,
            name: item.name,
            polygon: {
                hierarchy: {
                    //层次
                    positions: Cesium.Cartesian3.fromDegreesArray(item.position),
                },
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                material: Cesium.Color.RED.withAlpha(0.5),
               // zIndex:1
            },
        })
        window.tilesets_WL_Group.push(wall_Wl);
    })
    window.tilesets_WL_Group.forEach((item,index)=>{
        item.show=false;
    })
    addDynamicFlow(119.14474435975427,45.91992776878534, 960,'一号点','一号边坡');
    addDynamicFlow(119.14523958953819,45.918211500743354, 960,'二号点','一号边坡');
    addDynamicFlow(119.14266112072549,45.91871107537886, 960,'三号点','一号边坡');
    addDynamicFlow(119.1255475289073,45.92177409816852, 960,'一号点','二号边坡');
    addDynamicFlow(119.12976523475682,45.927237755438846, 960,'二号点','二号边坡');

}



window.WlPoint_Group=[];
function AddWlPoint(lat,lon,height,name){
     window.WlPoint=viewer.entities.add({
        // fromDegrees（经度，纬度，高度，椭球，结果）从以度为单位的经度和纬度值返回Cartesian3位置
        position: Cesium.Cartesian3.fromDegrees(lat,lon,height),
        point: {
            // 点的大小（像素）
            pixelSize: 5,
            // 点位颜色，fromCssColorString 可以直接使用CSS颜色
            color: Cesium.Color.fromCssColorString('#ee0000'),
            // 边框颜色
            outlineColor: Cesium.Color.fromCssColorString('#fff'),
            // 边框宽度(像素)
            outlineWidth: 2,
            // 显示在距相机的距离处的属性，多少区间内是可以显示的
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 7500),
            // 是否显示
            show: true
        },
         label : {
            text : name,
             font : '14pt Source Han Sans CN',    //字体样式
             fillColor:Cesium.Color.BLACK,        //字体颜色
             backgroundColor:Cesium.Color.AQUA,    //背景颜色
             showBackground:true,                //是否显示背景颜色
             style: Cesium.LabelStyle.FILL,        //label样式
             outlineWidth : 2,
             verticalOrigin : Cesium.VerticalOrigin.CENTER,//垂直位置
             horizontalOrigin :Cesium.HorizontalOrigin.LEFT,//水平位置
             distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 7500),
             pixelOffset:new Cesium.Cartesian2(10,-50)            //偏移
        }
    });

     //添加点击时间
    var Click_Point = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);//viewer.scene.
    Click_Point.setInputAction(function(
        click) {

        //
        // let cartesian = viewer.camera.pickEllipsoid(click.position);
        // let cartographic = Cesium.Cartographic.fromCartesian(cartesian);
        // let lng = Cesium.Math.toDegrees(cartographic.longitude); // 经度
        // let lat = Cesium.Math.toDegrees(cartographic.latitude); // 纬度
        //
        var pick = viewer.scene.pick(click.position);
        // console.log(pick);
        if (pick.id !=null) {//pick || pick.id == "Entity"
            //console.log(res.position.x, res.position.y)
            if(pick.id._name ==Click_dd_name){
                console.log('asd');
                // viewer.flyTo(pick.id);

                $("#bpxx").show();
                $("#xxxx").show();
                // $("#moxing").hide();
                // videoshow(pick);
            }
        } else {
            console.log('不是');
        }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    window.WlPoint_Group.push(WlPoint);
    window.WlPoint_Group.forEach((item,index)=>{
        item.show=false;
    })
}
//添加动态点信息
//添加动态点信息
var BpPoint_data=[{name:"一号点",lat:119.11,lon:45.1112,lable:"一号边坡"},
    {name:"二号点",lat:119.12,lon:45.1112,lable:"一号边坡"},
    {name:"三号点",lat:119.13,lon:45.1113,lable:"一号边坡"},
    {name:"一号点",lat:119.14,lon:45.1114,lable:"二号边坡"},
    {name:"二号点",lat:119.15,lon:45.1115,lable:"二号边坡"},
]
//添加动态点
//添加动态点
var Click_Point;
function addDynamicFlow(lat,lon,height,name,lable){
// 消隐圆特效
    window.WlPoint=viewer.entities.add({
        position: Cesium.Cartesian3.fromDegrees(lat,lon,height),
        name: name,
        type:"Point545454",
        description:lable,
        clampToGround: true,
        ellipse: {
            semiMinorAxis: 40.0,
            semiMajorAxis: 40.0,
            height: height-25,
            //distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 19000),
            material: new Cesium.CircleFadeMaterialProperty({
                color: Cesium.Color.BLUE.withAlpha(0.5),
                speed: 2.0,
            })
        },
        billboard: {
            // 图像地址，URI或Canvas的属性
            image: './images/WeizhiBiaoji.png',
            // 设置颜色和透明度
            color: Cesium.Color.WHITE.withAlpha(1),
            // // 高度（以像素为单位）
            height: 60,
            // 宽度（以像素为单位）
            width: 60,
            //distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 19000),
            // // 是否显示
            // show: true
            //根据广告牌与相机的距离，获取或设置广告牌的远近缩放属性。
            scaleByDistance:new Cesium.NearFarScalar(300,1,2000,0.3)
        },

    })
    //添加点击时间
    Click_Point = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);//viewer.scene.
    Click_Point.setInputAction(function(
        click) {
        var pick = viewer.scene.pick(click.position);
        // console.log(pick);
        if (pick.id !=null) {//pick || pick.id == "Entity"
            console.log('pick.id',pick.id._name)
            if(pick.id._name ==Click_dd_name){//判断点击面
                $("#bpxx").show();
                // $("#xxxx").show();
                $('#bianpotu').show();
            }else if (pick.id._type ==="Point545454"){
                $('#BpPoint_information_div').show();
                BpPoint_data.forEach((item,index)=>{
                    if(item.name==pick.id._name){//判断点击点
                        console.log(item.lat,item.lon)
                        htmlstr1='';
                        htmlstr1 += '<li >' +item.name+'</li>';
                        htmlstr1 += '<li >' +"边坡点号："+item.name+'</li>';
                        htmlstr1 += '<li >' +"经度："+item.lon+ '</li>';
                        htmlstr1 += '<li >' +"维度："+item.lat+ '</li>';
                        htmlstr1 += '<li >' +"所属边坡："+item.lable+ '</li>';
                        htmlstr1 += '</il>';
                        $("#BpPoint_information_ul").html(htmlstr1);
                    }
                })
            }
        } else {
            console.log('不是');
        }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    window.WlPoint_Group.push(WlPoint);
    window.WlPoint_Group.forEach((item,index)=>{
        item.show=true;
    })
}



var Click_dd_name='一号边坡';

//添加按钮点击事件显示边坡围栏
$(document).ready(function (){
    $('#bpmz div:nth-child(2) ul li #one_all dd').click(function (){
        Close_Bp_DataDiv();//关闭页面信息
        var dd_name= $(this).attr("name");
        $('#bpmz div:nth-child(2) ul li #one_all dd a').css('color','rgb(205, 223, 233)');
        $($(this).children(a)).css('color','rgb(177, 187, 126)');
        Click_dd_name=dd_name;
        console.log(dd_name);
        if(dd_name!=null){
            window.tilesets_WL_Group.forEach((item,index)=>{

                if(item.name==dd_name){
                    item.show=true;
                    viewer.flyTo(item);
                }else{
                    item.show=false;
                }
            })

            window.WlPoint_Group.forEach((item,index)=>{
                console.log(item.description._value)
                if(item.description._value.search(dd_name)!=-1){
                    item.show=true;
                    //viewer.flyTo(item);
                }else{
                    item.show=false;
                }
            })
        }else{
            console.log("不是44");
        }
        var flag1 = $('#bpxx').css('display') == "none";
        var flag2 = $('#bpxx1').css('display') == "none";
        // if(flag1&&flag2){
        // close_WL();
        // }
    });
})
//关闭必边坡围栏
function close_WL(){
    window.tilesets_WL_Group.forEach((item,index)=>{
        item.show=false;
    })
    window.WlPoint_Group.forEach((item,index)=>{
        item.show=false;
    })
    //Click_Point.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)//移除事件
}

//添加边坡点位详细列表
function Add_BpPoint_information(e) {
    $('#BpPoint_information_div').show();
    huanyuan_bpxx_color();
    $(e).css('color','rgb(177, 187, 126)');
    var Bp_div_name= $(e).attr("name");
    BpPoint_data.forEach((item,index)=>{
        if(item.name==Bp_div_name){
            console.log(item.lat,item.lon)
            htmlstr1='';
            htmlstr1 += '<li >' +item.name+'</li>';
            htmlstr1 += '<li >' +"边坡点号："+item.name+'</li>';
            htmlstr1 += '<li >' +"经度："+item.lon+ '</li>';
            htmlstr1 += '<li >' +"维度："+item.lat+ '</li>';
            htmlstr1 += '<li >' +"所属边坡："+item.lable+ '</li>';
            htmlstr1 += '</il>';
            $("#BpPoint_information_ul").html(htmlstr1);
        }
    })
}
function huanyuan_bpxx_color(){
    $('.bpxx div:nth-last-child(n+2)').css('color','rgb(205, 223, 233)');

}
function Close_Bp_DataDiv(){
    // var flag1 = $('#bpxx').css('display') == "none";
    $('#bpxx1').hide();
    $('#bpxx').hide();
    $('#BpPoint_information_div').hide();
    $('#bianpotu').hide();
    // var flag2 = $('#bpxx1').css('display') == "none";
}

//地形裁剪
function undergroundSpace() {
    //裁剪
    let globe = viewer.scene.globe;
    //globe.depthTestAgainstTerrain = true;
    let position = Cesium.Cartographic.toCartesian(new Cesium.Cartographic.fromDegrees(119.11327902002924,45.92727997972318, -200));
    globe.clippingPlanes = new Cesium.ClippingPlaneCollection({
        modelMatrix: Cesium.Transforms.eastNorthUpToFixedFrame(position),
        planes: [
            new Cesium.Plane(new Cesium.Cartesian3(1, 0.0, 0.0), -1500.0),
            new Cesium.Plane(new Cesium.Cartesian3(-1.0, 0.0, 0.0), -1500.0),
            new Cesium.Plane(new Cesium.Cartesian3(0.0, 1.0, 0.0), -1520.0),
            new Cesium.Plane(new Cesium.Cartesian3(0.0, -1.0, 0.0), -1520.0),
        ],
        edgeWidth: 1.0,
        edgeColor: Cesium.Color.BLACK
    });
}



